<!DOCTYPE html>
<html>

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>自动选项卡</title>
    <style>
        li {
            list-style: none;
            cursor: pointer;
        }

        ul {
            margin: 0;
            padding: 0;
        }

        .wrap {
            height: 800px;
            width: 1200px;
            overflow: hidden;
            margin: 0 auto;
            background-image: url(./images/bg.png);
            background-repeat: no-repeat;
        }

        #box {
            height: 397px;
            width: 800px;
            overflow: hidden;
            margin: 0 auto;
            margin-top: 228px;
            background: #ffb746;
        }

        #box>ul {
            width: 100%;
            height: 60px;
            display: flex;
        }

        #box>ul li {
            line-height: 60px;
            flex: 1;
            text-align: center;
            background-color: #fff;
            color: #000;
        }

        #box>ul .active {
            background-color: #ffb746;
            color: #fff;
        }

        #box div {
            width: 70%;
            height: 60%;
            margin: 40px auto;
            display: none;
            position: relative;
        }

        #box img {
            width: 250px;
            height: auto;
        }

        #box div ul {
            position: absolute;
            top: 0;
            left: 250px;
        }

        #box div li {
            color: white;
            text-align: left;
            padding: 20px 40px;
        }

        #box div .active {
            background-color: #fff;
            color: #000;
        }

        #box .com_content {
            display: none;
        }
    </style>

</head>

<body>
    <div class="wrap">
        <div id="box">
            <ul class="navs">
                <li class="active">暖色调</li>
                <li>金色夕阳</li>
                <li>灰色格调</li>
                <li>baby</li>
            </ul>
            <div style="display: block;" class="contW">
                <!-- <img src="images/1.png" style="display: block">
                <img src="images/2.png">
                <img src="images/3.png">
                <img src="images/4.png"> -->
                <p class="com_content" style="display: block">第一部分图片1</p>
                <p class="com_content">第一部分图片2</p>
                <p class="com_content">第一部分图片3</p>
                <p class="com_content">第一部分图片4</p>
            </div>
            <div class="contW">
                <!-- <img src="images/5.png" style="display: block">
                <img src="images/1.png"> -->
                <p class="com_content" style="display: block">第二部分图片1</p>
                <p class="com_content">第二部分图片2</p>
            </div>
            <div class="contW">
                <p class="com_content" style="display: block">第三部分图片1</p>
                <p class="com_content">第三部分图片2</p>
                <p class="com_content">第三部分图片3</p>
                <p class="com_content">第三部分图片4</p>
            </div>
            <div class="contW">
                <p class="com_content" style="display: block">第四部分图片1</p>
                <p class="com_content">第四部分图片2</p>
            </div>
        </div>
    </div>
    <script>

        /***
         *   封装TabFn的时候 不要使用this ,直接用当前索引，这样可以给定时器复用
         * ***/
        var box = $('#box');
        var aLi = $$('#box>ul li');
        var contW = $$('.contW');
        var num = 0;
        var prev = 0;

        clickTabFn();
        autoTab();
        iStopFn(); //是否停止定时器

        function autoTab() {
           autoTab.timer = setInterval( function(){
               num ++;
               num = num % aLi.length;
              TabFn( num );
           },1000);
        }
    
        function clickTabFn() {
           for( var i=0;i<aLi.length;i++ ) {
             aLi[i].index = i;
             aLi[i].onclick = function() {
                TabFn( this.index );
                num = this.index; // 重定向下定时器当前的索引
             }
           } 
        }

        function iStopFn() {
            box.onmouseover = function () {
                clearInterval( autoTab.timer );
            }
            box.onmouseout = function () {
                autoTab();
            }
        }
        function TabFn( index ) {
            aLi[prev].classList.remove('active');
            contW[prev].style.display ="none";
            aLi[index].classList.add('active');
            contW[index].style.display ="block";
            prev = index;
        }
        function $( obj ) {
            return document.querySelector( obj );
        }
        function $$( obj ) {
            return document.querySelectorAll(obj);
        }
    </script>
</body>

</html>